<!--评论模块-->
<template>
  <div class="container">
    <el-row :gutter="10">
      <el-col :xs="1" :sm="1" :md="4" :lg="6" :xl="6">
        <div class="grid-content bg-purple"></div>
      </el-col>
      <el-col :xs="22" :sm="22" :md="16" :lg="12" :xl="12">
        <div class="grid-content bg-purple-light">
          <el-row :gutter="20">
            <el-col :span="24">
              <div class="grid-content bg-purple">
                <div class="comment" v-for="item in comments">
                  <div class="info">
                    <img class="avatar" :src="item.fromAvatar" width="36" height="36">
                    <div class="right">
                      <div class="name">{{item.fromName}}</div>
                      <div class="date">{{item.date}}</div>
                    </div>
                  </div>
                  <div class="content">{{item.content}}{{text}}</div>
                  <div class="control">
                    <span class="like" :class="{active: item.isLike}" @click="likeClick(item)">
                      <i class="el-icon-thumb"></i>
                      <span class="like-num">{{item.likeNum > 0 ? item.likeNum + '人赞' : '赞'}}</span>
                    </span>
                    <span class="comment-reply" @click="showCommentInput(item)">
                      <i class="el-icon-chat-square"></i>
                      <span>回复</span>
                    </span>
                  </div>
                  <div class="reply">
                    <div class="item" v-for="reply in item.reply">
                      <div class="reply-content">
                        <span class="from-name">{{reply.fromName}}</span>
                        <span>:</span>
                        <span class="to-name">@{{reply.toName}}</span>
                        <span>{{reply.content}}</span>
                      </div>
                      <div class="reply-bottom">
                        <span>{{reply.date}}</span>
                        <span class="reply-text" @click="showCommentInput(item, reply)">
                          <i class="iconfont icon-comment"></i>
                          <span>回复</span>
                        </span>
                      </div>
                    </div>
                    <div
                      class="write-reply"
                      v-if="item.reply.length > 0"
                      @click="showCommentInput(item)"
                    >
                      <i class="el-icon-edit"></i>
                      <span class="add-comment">添加新评论</span>
                    </div>
                    <transition name="fade">
                      <div class="input-wrapper" v-if="showItemId === item.id">
                        <el-input
                          class="gray-bg-input"
                          v-model="inputComment"
                          type="textarea"
                          :rows="3"
                          autofocus
                          placeholder="写下你的评论"
                        ></el-input>
                        <div class="btn-control">
                          <span class="cancel" @click="cancel">取消</span>
                          <el-button type="success" round @click="commitComment">确定</el-button>
                        </div>
                      </div>
                    </transition>
                  </div>
                </div>
              </div>
            </el-col>
          </el-row>
        </div>
      </el-col>
      <el-col :xs="1" :sm="1" :md="4" :lg="6" :xl="6">
        <div class="grid-content bg-purple-light"></div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import Vue from "vue";

export default {
  props: {
    comments: {
      type: Array,
      required: true
    }
  },
  components: {},
  data() {
    return {
      text:'',
      inputComment: "",
      showItemId: ""
    };
  },
  computed: {},
  methods: {
    // 回复
    commitComment(){
      this.text.push(this.inputComment)
    },
    /**
     * 点赞
     */
    likeClick(item) {
      if (item.isLike === null) {
        Vue.$set(item, "isLike", true);
        item.likeNum++;
      } else {
        if (item.isLike) {
          item.likeNum--;
        } else {
          item.likeNum++;
        }
        item.isLike = !item.isLike;
      }
    },

    /**
     * 点击取消按钮
     */
    cancel() {
      this.showItemId = "";
    },

    /**
     * 提交评论
     */
    commitComment() {
      console.log(this.inputComment);
    },

    /**
     * 点击评论按钮显示输入框
     * item: 当前大评论
     * reply: 当前回复的评论
     */
    showCommentInput(item, reply) {
      if (reply) {
        this.inputComment = "@" + reply.fromName + " ";
      } else {
        this.inputComment = "";
      }
      this.showItemId = item.id;
    }
  },
  created() {
    console.log(this.comments);
  }
};
</script>

<style scoped lang="scss">
//   @import "../../public/scss/index";

.container {
  margin-right: 20px;
  box-sizing: border-box;
  .comment {
    display: flex;
    flex-direction: column;
    /*padding: 10px;*/
    border-bottom: 1px solid #f2f6fc;
    .info {
      display: flex;
      align-items: center;
      .avatar {
        border-radius: 50%;
      }
      .right {
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        .name {
          font-size: 16px;
          color: #303133;
          margin-bottom: 5px;
          font-weight: 500;
        }
        .date {
          font-size: 12px;
          color: #909399;
        }
      }
    }
    .content {
      font-size: 16px;
      color: #303133;
      line-height: 20px;
      /*padding: 10px 0;*/
    }
    .control {
      display: flex;
      align-items: center;
      font-size: 14px;
      color: #909399;
      .like {
        display: flex;
        align-items: center;
        /*margin-right: 20px;*/
        cursor: pointer;
        &.active,
        &:hover {
          color: #409eff;
        }
        .iconfont {
          font-size: 14px;
          /*margin-right: 5px;*/
        }
      }
      .comment-reply {
        display: flex;
        align-items: center;
        cursor: pointer;
        &:hover {
          color: #333;
        }
        .iconfont {
          font-size: 16px;
          margin-right: 5px;
        }
      }
    }
    .reply {
      margin: 10px 0;
      border-left: 2px solid #dcdfe6;
      .item {
        margin: 0 10px;
        /*padding: 10px 0;*/
        border-bottom: 1px dashed #ebeef5;
        .reply-content {
          display: flex;
          align-items: center;
          font-size: 14px;
          color: #303133;
          .from-name {
            color: #409eff;
          }
          .to-name {
            color: #409eff;
            /*margin-left: 5px;*/
            /*margin-right: 5px;*/
          }
        }
        .reply-bottom {
          display: flex;
          align-items: center;
          margin-top: 6px;
          font-size: 12px;
          color: #909399;
          .reply-text {
            display: flex;
            align-items: center;
            /*margin-left: 10px;*/
            cursor: pointer;
            &:hover {
              color: #333;
            }
            .icon-comment {
              margin-right: 5px;
            }
          }
        }
      }
      .write-reply {
        display: flex;
        align-items: center;
        font-size: 14px;
        color: #909399;
        padding: 10px;
        cursor: pointer;
        &:hover {
          color: #303133;
        }
        .el-icon-edit {
          margin-right: 5px;
        }
      }
      .fade-enter-active,
      fade-leave-active {
        transition: opacity 0.5s;
      }
      .fade-enter,
      .fade-leave-to {
        opacity: 0;
      }
      .input-wrapper {
        padding: 10px;
        //   .gray-bg-input, .el-input__inner {
        //     /*background-color: #67C23A;*/
        //   }
        .btn-control {
          display: flex;
          justify-content: flex-end;
          align-items: center;
          padding-top: 10px;
          .cancel {
            font-size: 16px;
            color: #606266;
            margin-right: 20px;
            cursor: pointer;
            &:hover {
              color: #333;
            }
          }
          .confirm {
            font-size: 16px;
          }
        }
      }
    }
  }
}

/* .el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
} */
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  background: #99a9bf;
}

.bg-purple-light {
  height: 13px;
  /* background: #e5e9f2; */
}
.grid-content {
  border-radius: 4px;
  min-height: 1px;
}
.row-bg {
  padding: 10px 0;
  /* background-color: #f9fafc; */
}
</style>
